﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Reflection;
using EasyQuartz.Domain.Services.Scheduling;
using Quartz;

namespace EasyQuartz.Server.WebServices.Scheduler
{
	/// <summary>
	/// Summary description for SchedulerService
	/// </summary>
	[WebService(Namespace = "http://tempuri.org/")]
	[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
	[System.ComponentModel.ToolboxItem(false)]
	// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
	// [System.Web.Script.Services.ScriptService]
	public class SchedulerService : System.Web.Services.WebService
	{
		[WebMethod]
		public AddJobResult AddJob(AddJobArgs args)
		{
			var result = new AddJobResult { Succefull = false };

			SchedulingManager manager = new SchedulingManager();
			manager.AddJob(args.EasyJob.Name, args.EasyJob.Group, args.EasyJob.CronExpression, args.EasyJob.Type);

			result.Succefull = true;

			return result;
		}

		[WebMethod]
		public AddJobsResult AddJobs(AddJobsArgs args)
		{
			var result = new AddJobsResult { Succefull = false };
			
			SchedulingManager manager = new SchedulingManager();
			foreach (var job in args.Jobs)
			{
				try
				{
					manager.AddJob(job.Name, job.Group, job.CronExpression, job.Type);
				}
				catch (Exception)
				{
					string.Concat(result.Message, "Error on ", job.Name);
				}
			}

			// TODO: check warnings error
			result.Succefull = true;

			return result;
		}
	}
}
